package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.constants.RedisConstants;
import cn.gyx.hrm.domain.VipUser;
import cn.gyx.hrm.dto.SMSCodeDto;
import cn.gyx.hrm.exception.GlobalException;
import cn.gyx.hrm.mapper.VipUserMapper;
import cn.gyx.hrm.service.IVipUserService;
import cn.gyx.hrm.service.VipVerifycodeService;
import cn.gyx.hrm.to.SendVerifyCodeTo;
import cn.gyx.hrm.util.AssertUtil;
import cn.gyx.hrm.util.StrUtils;
import cn.gyx.hrm.util.VerifyCodeUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class VipVerifycodeServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements VipVerifycodeService {
   @Autowired
   private RedisTemplate redisTemplate;

   // 图片验证码
    @Override
    public String creatImgcode(String key) {
        // 断言是否为空
        AssertUtil.isNotEmpty(key,"非法请求" );
//        if (StringUtils.isEmpty(key)){
//            throw new GlobalException("非法请求");
//        }
        // 随机生成图片验证码
        String image = StrUtils.getComplexRandomString(4);
        // 把生成的图片验证码存入redis中
        redisTemplate.opsForValue().set(key,image ,10L , TimeUnit.MINUTES);
        try {
            return VerifyCodeUtils.VerifyCode(130,35 ,image );
        } catch (IOException e) {
            e.printStackTrace();
            throw  new GlobalException("图片验证码请求错误");
        }
    }

    // 短信验证
    @Override
    public void sendSmsCode(SMSCodeDto dto) {
        String imageCode = dto.getImageCode(); // 获取图片验证
        String imageCodeKey = dto.getImageCodeKey();// 获取图片验证码key值
        String mobile = dto.getMobile();// 获取用户电话

        // 1.判断获取值是否为空
        AssertUtil.isNotEmpty(imageCode,"验证码获取失败" );
        AssertUtil.isNotEmpty(imageCodeKey,"非法请求" );
        AssertUtil.isNotEmpty(mobile,"用户电话获取失败");

        // 2.判断图片验证码与redis中是否一致
        String imageCodeKeyFromRedis = (String)redisTemplate.opsForValue().get(imageCodeKey);
        // 判断获取的图形验证码是否为空
        AssertUtil.isNotEmpty(imageCodeKeyFromRedis, "图形验证码不能为空");
        // 比较用户输入验证和redis中是否一致
        AssertUtil.isEquals(imageCodeKeyFromRedis, imageCode,"图片验证码验证失败");

        String code = null;
        // 3.如果短信验证码有上一次
        String keySmsCode = String.format(RedisConstants.KEY_SMS_CODE, mobile);
        // 获取上一次redis中数据
        SendVerifyCodeTo lastSend = (SendVerifyCodeTo)redisTemplate.opsForValue().get(keySmsCode);
        // 获取系统时间
        long Time = new Date().getTime();
        // 判断获取系统时间是否大于60s
        if (lastSend != null){
            if (Time - lastSend.getSendTime()<6000) {
                throw new GlobalException("验证码已发送，请勿频繁操作");
            } else {
                code = lastSend.getCode();
            }
        } else {
            // 如果短信验证码没有上一次
            // 随机生成手机验证码先
            code = StrUtils.getRandomString(6);
        }
        // 网关发送短信
        log.info("发送的短信验证码为：{} 过期时间为：{}分钟",code,10L);

        // 将数据放到redis中
        redisTemplate.opsForValue().set(keySmsCode,new SendVerifyCodeTo(code,new Date().getTime()));

        // 删除图片验证码
        redisTemplate.delete(imageCodeKey);
    }
}